#include "door.h"
#include "main.h"

#define CIRCULAR_DOUBLE_DOOR whole_machine_parameter.machine_type_id == circular_double_door



door::door(char door_number,QObject *parent) :
    door_number(door_number),QObject(parent)
{

    timer=new QTimer();
    QObject:: connect( timer, SIGNAL(timeout()), SLOT(timeDone()) );
    timer->start(100);//监控门到位信号，越短越好
    timer2=new QTimer();
    QObject:: connect( timer2, SIGNAL(timeout()), SLOT(timeDone2()) );
    timer2->start(1000);
    single_timer=new QTimer();
    single_timer->setSingleShot(true);
    QObject:: connect( single_timer, SIGNAL(timeout()), SLOT(single_timeDone()) );
    action=0;
    inner_pumpdown_timer=new QTimer;
    QObject:: connect( inner_pumpdown_timer, SIGNAL(timeout()), SLOT(stop_inner_pumpdown()) );
    QTimer::singleShot(5000, this, SLOT(custom_slot()));
   // c=true;重启后无法门到位时自动锁定
    s=1;

    door_Sealing_strip_Inflation_delay_timer=new QTimer();
    door_Sealing_strip_Inflation_delay_timer->setSingleShot(true);
    QObject:: connect( door_Sealing_strip_Inflation_delay_timer, SIGNAL(timeout()), SLOT(door_Sealing_strip_Inflation_delay_timeDone()) );
    stop_door_Sealing_strip_Inflation_delay_reached=false;

    stop_door_Sealing_strip_Inflation_delay_timer=new QTimer();
    stop_door_Sealing_strip_Inflation_delay_timer->setSingleShot(true);
    QObject:: connect( stop_door_Sealing_strip_Inflation_delay_timer, SIGNAL(timeout()), SLOT(stop_door_Sealing_strip_Inflation_delay_timeDone()) );
    stop_door_Sealing_strip_Inflation_delay_reached=false;
    door_seal_type_changed=false;
}

void door::custom_slot()
{
    s=0;
}

void door::open()
{
         c=false;
        stop_close_door();
        if(CIRCULAR_DOUBLE_DOOR)
        {
          stop_door_Sealing_strip_Inflation() ;
          door_Sealing_strip_Exhaust();//圆炉双门
         }
         //door_actions=openning_front_door;
         action=Sealing_strip_Exhaust;

 if(CIRCULAR_DOUBLE_DOOR)
 {
    single_timer->start(door_Sealing_strip_Exhaust_time_ms);
 }
  else
  {
    if( door_closed_in_place)
    {
        Vacuum_pump_ON

      if(whole_machine_parameter.bottom_exhaust_machine)
        {
            stop_door_Sealing_strip_Inflation();
            single_timer->start(bottom_door_Sealing_strip_Exhaust_time_ms);
          //  WARN->show_text(QString::number(bottom_door_Sealing_strip_Exhaust_time_ms).toAscii());
        }
      else inner_pumpdown_timer->start(500);
        counter=0;

    }
    else // 放炉自动门开到一半时不需要再抽气
    {
      open_door();
      single_timer->start(door_action_time_ms );
      action=Open_Door ;
    }


  }


}


void door::close()
{

    stop_door_Sealing_strip_Exhaust();
    if(CIRCULAR_DOUBLE_DOOR)
    {
     door_Sealing_strip_Inflation() ;
    }
    else  if( !door_closed_in_place  )
    {
        close_door() ;
        stop_door_Sealing_strip_Inflation();
        Vacuum_pump_ON
        exhaust_valve_OFF
       // single_timer->stop();//停止开门排气定时器
        single_timer->start(door_action_time_ms );
        action=Door_Actions;
    }

    c=true;

}


void door::stop_inner_pumpdown()
{

    if(PSU < -4 ||!door_Sealing_strip_pressure_in_place ||counter++==59  )
    {
    exhaust_valve_OFF
    air_supply_valve_ON
    stop_door_Sealing_strip_Inflation();
    single_timer->start(vacuum_door_Sealing_strip_Exhaust_time_ms);
    inner_pumpdown_timer->stop();
    }
}

void door::timeDone()
{

    if(door_opened_in_place)
    {
         stop_open_door() ;
    }

     if( door_closed_in_place  )
    {
        stop_close_door() ;
        if(action==Door_Actions) {Vacuum_pump_OFF}
        if(door_Sealing_strip_Inflation_delay_reached && c )//双门圆炉前门解锁时马上又锁定，后门无此现象
        {   //原因是C++对象排列问题，排列不整齐，读取出错，添加一个Bool对象后问题消失
        stop_door_Sealing_strip_Exhaust();
        door_Sealing_strip_Inflation() ;
        c=false;
        door_Sealing_strip_Inflation_delay_reached=false;
        }
        if(!door_Sealing_strip_pressure_in_place && !door_Sealing_strip_Inflation_delay_timer->isActive() )
            door_Sealing_strip_Inflation_delay_timer->start(1000);
        if(stop_door_Sealing_strip_Inflation_delay_timer->isActive() )
                stop_door_Sealing_strip_Inflation_delay_timer->stop();
    }
     else
     {
         if( action==Open_Door ) Vacuum_pump_OFF
         if(door_operation->isVisible() ) air_supply_valve_OFF
        //在门控制界面才执行，以防在单门设备中运行时关闭回气阀
         if(door_Sealing_strip_Inflation_delay_timer->isActive() )
                 door_Sealing_strip_Inflation_delay_timer->stop();

         if( !stop_door_Sealing_strip_Inflation_delay_timer->isActive() )
             stop_door_Sealing_strip_Inflation_delay_timer->start(1000);
        if(stop_door_Sealing_strip_Inflation_delay_reached)
        {stop_door_Sealing_strip_Inflation();stop_door_Sealing_strip_Inflation_delay_reached=false;}
     }
     if(CIRCULAR_DOUBLE_DOOR)//圆炉双门 门锁到位时停止供电
     {
         if(door_Sealing_strip_pressure_in_place) stop_door_Sealing_strip_Inflation();
     }

     if(door_number)
     {
       door_closed_in_place=front_door_closed_in_place;
       door_opened_in_place=front_door_opened_in_place;
       door_Sealing_strip_pressure_in_place=front_door_Sealing_strip_pressure_in_place;
     }
     else
     {
         door_closed_in_place=back_door_closed_in_place;
         door_opened_in_place=back_door_opened_in_place;
         door_Sealing_strip_pressure_in_place=back_door_Sealing_strip_pressure_in_place;
     }
}

void door::timeDone2()
{
    /**a=0 c=0
       a=1 c=!b **/
   //if(CIRCULAR_DOUBLE_DOOR || s)
    if(action!=Sealing_strip_Exhaust && action!=Door_Actions)
   {
//    a= door_closed_in_place;
//    if(a^b) {c=a; b=!b;}
//    else c=false;
      if(door_closed_in_place && !a) c=true;
      if(a!=door_closed_in_place) a=door_closed_in_place;
   }

    if(door_number)
    {
      open_door_pin=open_front_door_pin;
      close_door_pin=close_front_door_pin;
      door_Sealing_strip_Inflation_pin=front_door_Sealing_strip_Inflation_pin;
      door_Sealing_strip_Exhaust_pin=front_door_Sealing_strip_Exhaust_pin;
    }
    else
    {
        open_door_pin=open_back_door_pin;
        close_door_pin=close_back_door_pin;
        door_Sealing_strip_Inflation_pin=back_door_Sealing_strip_Inflation_pin;
        door_Sealing_strip_Exhaust_pin=back_door_Sealing_strip_Exhaust_pin;
    }
    /**进入升温阶段后，改压缩空气密封为蒸汽密封***/
    if(sterilization_stage==2||sterilization_stage==3)
    {
        if(PSU>80 )
        {/**切换过程出现门封不到位情况**/
            if(!door_detection_failure ) door_detection_failure=true;
            if(PSU>85 && door_detection_failure )
            {door_detection_failure=false;}
            stop_door_Sealing_strip_Inflation();
            if(!door_seal_type_changed) door_seal_type_changed=true;
        }
    }/***恢复压缩空气密封*****/
    else //if(sterilization_stage==4 && !door_Sealing_strip_pressure_in_place)
        if(door_closed_in_place && action!=Sealing_strip_Exhaust && action!=Door_Actions )
         {
            door_Sealing_strip_Inflation();
            if(door_seal_type_changed && door_detection_failure )
            {door_detection_failure=false;door_seal_type_changed=false;}
        }
}


void door::single_timeDone()
{
    switch(action)
    {
    case Sealing_strip_Exhaust ://开门胶条排气等待

           stop_door_Sealing_strip_Exhaust();//双门圆炉解锁10s到位后停止供电
                 // Vacuum_pump_OFF
           if(!door_Sealing_strip_pressure_in_place)
           {
               open_door();
               single_timer->start(door_action_time_ms );
               action=Door_Actions ;
             //  WARN->show_text("guitarhua");
           }

            break;
    case Open_Door:
    case Door_Actions ://门动作计时到停止
            Vacuum_pump_OFF
            stop_open_door();
            stop_close_door();
            action=0;
                break;
    default:break;
    }

}

void door::door_Sealing_strip_Inflation_delay_timeDone()
{
     if( door_closed_in_place )door_Sealing_strip_Inflation_delay_reached=true;
     else door_Sealing_strip_Inflation_delay_reached=false;
}

void door::stop_door_Sealing_strip_Inflation_delay_timeDone()
{
     if( door_closed_in_place ) stop_door_Sealing_strip_Inflation_delay_reached=false;
     else stop_door_Sealing_strip_Inflation_delay_reached=true;
}

void door::open_door()
{
  stop_close_door();
  monitor_program->set_output_pin(open_door_pin,true);

}

void door::stop_open_door()
{
  monitor_program->set_output_pin(open_door_pin,false);

}

void door::close_door()
{
  stop_open_door();
  monitor_program->set_output_pin(close_door_pin,true);

}

void door::stop_close_door()
{
  monitor_program->set_output_pin(close_door_pin,false);

}

void door::door_Sealing_strip_Inflation()
{
  monitor_program->set_output_pin(door_Sealing_strip_Inflation_pin,true);

}

void door::stop_door_Sealing_strip_Inflation()
{
    monitor_program->set_output_pin(door_Sealing_strip_Inflation_pin,false);
}

void door::door_Sealing_strip_Exhaust()
{
  monitor_program->set_output_pin(door_Sealing_strip_Exhaust_pin,true);

}


void door::stop_door_Sealing_strip_Exhaust()
{
    monitor_program->set_output_pin(door_Sealing_strip_Exhaust_pin,false);
}
